<template>
	<view>
		<view class="search-content">
			<view class="search">
				<input type="text" placeholder-class="inputclass" confirm-type="search" focus="true"
					placeholder="请输入关键字" v-model="searchVal" @confirm="onKeyInput" />


			</view>
			<view class="search-code" @click="handleSearch">搜索</view>
		</view>
		<!-- 搜索历史 -->
		<!-- v-show="historyFlag" -->
		<view class="search-history" v-show="historyFlag">
			<view class="search-title">
				<view>
					搜索历史
				</view>
				<view @click='removeStorage()'>
					<image src="../../static/coen/searchend.svg" mode="widthFix"></image>
				</view>
			</view>
			<view class="menu-block">

				<block v-for='(item,i) in setData' :key='i'>
					<view @click="select(item)">{{item}}

					</view>
				</block>
			</view>

		</view>
		<!-- 搜索内容 -->
		<Takeout :getWxshopList='searchList' />
		<Tips ref='show_tips' />
	</view>
</template>

<script>
	import {
		search
	} from '../../api/api/forshop.js'
	import Takeout from '../index/components/takeout.vue'
	import Tips from '../../element/tips.vue'
	export default {
		components: {
			Takeout,
			Tips
		},
		data() {
			return {
				searchVal: '',
				searchList: [],
				setData: [],
				historyFlag: false
			}
		},
		onLoad() {
			this.getSearchKey()
			this.historyFlag = true
		},
		methods: {
			select(item) {
				console.log(item)
				this.searchVal = item

			},
			//搜索
			handleSearch() {
				let val = this.searchVal
				this.search(val)
				this.setSearchKey(val)
				this.getSearchKey()
			},
			onKeyInput(e) {
				let val = e.detail.value
				this.search(val)
				this.setSearchKey(val)
				this.getSearchKey()
			},
			async search(val) {
				if (val) {
					const {
						data: res
					} = await search({
						searchdata: val
					})

					if (res.constructor !== Array) {
						this.historyFlag = true
						this.showtips(true, res)

					} else {
						this.historyFlag = false
						this.showtips(false)
					}

					this.searchList = res

				}


			},
			setSearchKey(key) {
				if (key) {
					let storage = uni.getStorageSync('search_key') || []
					console.log('stor', storage)
					storage.unshift(key)

					uni.setStorageSync('search_key', storage)
				}

			},
			getSearchKey() {
				this.setData = [...new Set(uni.getStorageSync('search_key') || [])]
			},
			removeStorage() {
				uni.removeStorageSync('search_key')
				this.getSearchKey()
			},
			//显示无货提示
			showtips(flag, text) {
				this.$refs.show_tips.show(flag, text)
				// this.$nextTick(()=>{
				// 	this.refs.show_tips.show(flag, text)
				// })
			}

		}
	}
</script>

<style lang="scss" scoped>
	.search-content {
		display: flex;
		justify-content: space-between;
		height: 70upx;
		align-items: center;
		// line-height: 70upx;
		padding: 30upx 0;

		.search {
			display: flex;
			justify-content: space-between;
			height: 70upx;
			line-height: 70upx;
			width: 100%;
			// flex-direction: row;
			display: flex;
			border-radius: 20upx;
			background: #f8f8f8;
			margin-left: 20upx;

			input {
				height: 70upx;
				line-height: 70upx;
				width: 100%;
				font-size: 30upx;
				color: #666666;
				padding-left: 30upx;
			}
		}

		.search-code {
			width: 150upx;
			height: 50upx;
			text-align: center;
			font-size: 30upx;
		}
	}

	.search-history {
		margin: 20upx;

		.search-title {
			font-size: 30upx;
			font-weight: bold;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 60upx;
			line-height: 60upx;

			image {
				width: 36upx;
				height: 36upx;
				display: block;
			}
		}

		.menu-block {
			display: flex;
			justify-content: flex-start;
			flex-wrap: wrap;

			view {
				background: #f7f8fa;
				border-radius: 6upx;
				font-size: 27upx;
				color: #292c33;
				text-align: center;
				margin: 10upx;
			}
		}
	}
</style>
